package com.hama.iotrust.common.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.annotation.JSONField;

import com.hama.iotrust.common.ui.EasyUITree;
import com.hama.iotrust.common.ui.EasyUITreeNode;
import com.hama.iotrust.common.util.StrUtil;

public class EasyUITreeNode {

    public static final String  STATE_OPEN   = "open";
    public static final String  STATE_CLOSED = "closed";
    public static final String  STATE_NULL   = "";

    private String              id;
    private String              parentId;
    private String              text;
    private String              iconCls;
    private String              state        = STATE_NULL;
    private boolean             checked;
    private EasyUITree          children;
    private Map<String, Object> attributes;

    public EasyUITreeNode(String id, String parentId, String text){
        this.id = id;
        this.parentId = parentId;
        this.text = text;
    }

    public EasyUITreeNode getChildNodeById(String id) {
        if (children != null && children.size() > 0) {
            for (EasyUITreeNode subNode : children) {
                if (subNode.getId().equals(id)) {
                    return subNode;
                }
                EasyUITreeNode foundSubNode = subNode.getChildNodeById(id);
                if (foundSubNode != null) return foundSubNode;
            }
        }
        return null;
    }

    @JSONField(serialize = false)
    public List<String> getIdListOfChildrenWithSelf() {
        List<String> result = new ArrayList<String>();
        result.add(this.getId());
        if (children != null && children.size() > 0) {
            for (EasyUITreeNode subNode : children) {
                result.addAll(subNode.getIdListOfChildrenWithSelf());
            }
        }
        return result;
    }

    public void removeNode(String id) {
        if (children != null && children.size() > 0) {
            for (int i = children.size() - 1; i >= 0; i--) {
                EasyUITreeNode subNode = children.get(i);
                if (subNode.getId().equals(id)) {
                    children.remove(subNode);
                }
                subNode.removeNode(id);
            }
        }
    }

    public void addChildNode(EasyUITreeNode childNode) {
        if (children == null) children = new EasyUITree();
        children.add(childNode);
    }

    public void addAttribute(String key, Object value) {
        if (attributes == null) attributes = new HashMap<String, Object>();
        attributes.put(key, value);
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

    public boolean hasChildren() {
        return children != null && children.size() > 0;
    }

    public EasyUITree getChildren() {
        return children;
    }

    public void setChildren(EasyUITree children) {
        this.children = children;
    }

    public Map<String, Object> getAttributes() {
        return attributes;
    }

    public void setAttributes(Map<String, Object> attributes) {
        this.attributes = attributes;
    }

    public String getIconCls() {
        return iconCls;
    }

    public void setIconCls(String iconCls) {
        this.iconCls = iconCls;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    /**
     * @see java.lang.Object#toString()
     */
    public String toString() {
        return StrUtil.jsonFromObject(this);
    }

}
